<%@tag import="java.util.ArrayList"%>
<%@tag import="java.sql.Connection"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhTeam"%>
<%@tag import="by.epam.communalservices.entity.JkhTeam"%>
<%@tag import="java.util.List"%>

<%@ attribute name="name" required="true"%>
<%@ attribute name="selected" required="true"%>
<%@ attribute name="owner"%>
<%@ attribute name="attr"%>
<%@ attribute name="none"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<select name="${name}" ${attr}>
    <c:if test="${none ne ''}">
		<OPTION VALUE='' ${selected=='' ? 'SELECTED' : ''}><c:out value="выбрать бригаду"/></OPTION>
	</c:if>
	<%
		Long selected = 0L;
	    try {
	        selected = Long.parseLong(getJspContext().getAttribute("selected").toString());
	    } catch (NumberFormatException e) {
	    }
    	JdbcDaoJkhTeam teamDao = new JdbcDaoJkhTeam((Connection) request.getSession().getAttribute("currentConnection"));
        List<JkhTeam> teamList = teamDao.findAll();

    	Long owner = 0L;
    	try {
    	    owner = Long.parseLong( getJspContext().getAttribute("owner").toString() ); 
    	} catch (Exception e) {
    	}
    	if (owner>0) {
    		List<JkhTeam> newList = new ArrayList<JkhTeam>();
    	    for (JkhTeam item : teamList) {
    	        if (item.getIdOwner()==owner) {
    	            newList.add(item);
    	        }
    	    }
    	    teamList = newList;
    	}
        
        
        for (JkhTeam item : teamList) {
            out.println("<OPTION VALUE='" + item.getId() + "' " + (item.getId() == selected ? "SELECTED" : "") + ">"
                    + item.getId().toString()+ ". "+ item.getSurname() + " " + item.getName() + "</OPTION>");
        }
	%>

</select>
